// pages/page/home/home.js
const app = getApp()
Page({
  /**
   * 首页
   */
  data: {
    guid:'',
    houseinfodata:{},//获取到的住户总数据
    projectinfodata:{},//获取到的工程总数据
    jupaiDatas:{},//现场实景举牌数据
    recordDatas:[],//现场实景总数据 
    pdfDatas:[],//pdf文件列表
    comUrl:'',//文件访问公共地址
    tabs: ['主体验收', '竣工验收'],
    sjcurrent:0, //tabs 下标
    bgcurrent:0 //tabs 下标
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      comUrl: app.globalData.fileUrl
    });

    //测试的数据  
    //wx.setStorageSync('guid', '9fdda666-c04c-40a0-a330-98df0901febd');
    //console.log(app.globalData.userInfo);
    //无登录不处理 等appjs跳转
    /*if (app.globalData.userInfo==null) {
      wx.reLaunch({
        url: '../user/user'
      })
    }else*/ 
    //options.guid='39020408-f0ac-49ee-9901-160adce5db8c';
    if(options.guid!=null && options.guid.length>0){
        that.setData({
          guid: options.guid
        });
        this.initPages();
    }else{
      wx.showToast({
         title: '请先扫描分享码',
        icon: 'none',
        duration: 2000
      });
        setTimeout(function () {
          wx.reLaunch({
            url: '../scan/scan'
          });
        }, 1000);
    }
    
  },

  initPages:function(){
    if (app.globalData.userInfo != "null"){
      this.gethouseInfoData();
      this.getRoomInfoData();
      this.getPdfInfoData();
    }
  },

  gethouseInfoData:function(){
    var that = this;
    console.log(that.data.guid);
    wx.request({
      url: app.globalData.url + '/getAllHouseHoldInfo.do',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        guid: that.data.guid
      },
      success: function (res) {
        if (res.data.code==200){
          that.setData({
            houseinfodata: res.data.data.housedata,
            projectinfodata:res.data.data.sprodata
          })
        }else{
          wx.showToast({
            title: '暂无数据',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            houseinfodata: [],
            projectinfodata:[]
          })
        }
      }
    })
  },
  
  //获取厅室的信息  及上传的img 
  getRoomInfoData:function(){
    var that = this;
    that.getRoomJupaiInfoData();
    wx.request({
      url: app.globalData.url + '/getHallRoomPagesInfo.do',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        acceptanceType:that.data.sjcurrent+1,
        guid: that.data.guid,
      },
      success: function (res) {
        if (res.data.code==200){
          that.setData({
            recordDatas:res.data.data
          })
        }else{
          wx.showToast({
            title: '暂无数据',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            recordDatas:[]
          })
        }
      }
    })
  },
 
//现场实景 tab选择
photoTabSelect:function(e){
  var current = e.currentTarget.dataset.id;
  //带动pdf选择标
  this.setData({
    sjcurrent:current,
    bgcurrent:current
  })
  this.getRoomInfoData();
  this.getPdfInfoData();
},

  getPdfInfoData:function(){
    var that = this;
    wx.request({
      url: app.globalData.url + '/getRecordsByhouseHallGuid.do',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        guid: that.data.guid,
        type:that.data.bgcurrent+1,
      },
      success: function (res) {
        if (res.data.code==200){
          that.setData({
            pdfDatas: res.data.data,
          })
        }else{
          that.setData({
            pdfDatas: [],
          })
        }
      }
    })
  },
  //pdf选择tab
  pdfTabSelect:function(e){
    var current = e.currentTarget.dataset.id;
    this.setData({
      bgcurrent:current
    })
    this.getPdfInfoData();
  },

 //获取举牌验收信息
 getRoomJupaiInfoData:function(){
  var that = this;
  wx.request({
    url: app.globalData.url + '/findJupaiAvbaseByAcceptanceType.do',
    method: "POST",
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data: {
      acceptanceType:that.data.sjcurrent+3,
      guid: that.data.guid,
    },
    success: function (res) {
      if (res.data.code==200){
        that.setData({
          jupaiDatas:res.data.data
        })
      }else{
        that.setData({
          jupaiDatas:{avb_Id:0}
        })
      }
    }
  })
},
  

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.initPages();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },

   hxtImgView: function(e){
    var that=this;
    var imgArrays = [that.getComonUrl(2,that.data.houseinfodata.h_Id)];
    var url=that.getComonUrl(2,that.data.houseinfodata.h_Id);
    wx.previewImage({
     current: url, 
     urls: imgArrays 
    })
  },

  
  //查看举牌img
  jupaiImgView: function(){
    var that=this;
    var url=that.getComonUrl(1,that.data.jupaiDatas.avb_SliceName,that.data.jupaiDatas.avb_Id);
    var imgArrays = [url];
    var url=url;
    wx.previewImage({
     current: url, 
     urls: imgArrays 
    })
  },
  //查看img
  xcsjImgView: function(e){
    var that=this;
    var imgArrays = [];
    var url;
    var listindex=e.currentTarget.dataset.listindex; //list 数据的下标
    var avbindex=e.currentTarget.dataset.avbindex;//当前点击的照片下标
    var list=that.data.recordDatas[listindex].list1;
    if(list.length==0){
      return;
    }
    for(var i=0;i<list.length;i++){
      if(list[i]!=null || list[i] ==undefined || list[i]==""){
        if(list[i].avb_Id!=null && list[i].avb_Id>0){
          if(i==avbindex){
            url=that.getComonUrl(1,list[i].avb_SliceName,list[i].avb_Id);
          }
          imgArrays.push(that.getComonUrl(1,list[i].avb_SliceName,list[i].avb_Id));    
        }
      }
    }
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: imgArrays // 需要预览的图片http链接列表
    })
   
  },
//查看 pdf  img文件版
showPdf: function (e) {
  var that=this;
  var index=e.currentTarget.dataset.listindex;
  var recordindex=e.currentTarget.dataset.recordindex;
  var info=that.data.pdfDatas[index];
  var imgArrays = [];
  var url;
  console.log(info.record_PdfPage);
  for(var i=1;i<=info.record_PdfPage;i++){
    if(recordindex==i){
      url=that.getComonUrl(3,info.record_SliceId,info.record_Id,i);
    }
    imgArrays.push(that.getComonUrl(3,info.record_SliceId,info.record_Id,i));    
  }
  wx.previewImage({
    current: url, // 当前显示图片的http链接
    urls: imgArrays // 需要预览的图片http链接列表
  })
},
 /* showPdf: function (e) {
    var index=e.currentTarget.dataset.index;
    var info=this.data.pdfDatas[index];
    var url= this.getComonUrl(3,info.record_SliceId,info.record_Id);
      //打开pdf文件
      wx.showLoading({
        title: '加载中...',
      })
      wx.downloadFile({
        url: url,
        success: function (res) {
          const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            success: function (res) {
              wx.hideLoading()
            },
            fail: function (res) {
              wx.hideLoading();
              wx.showToast({
                title: '文档不存在,打开失败.',
                icon: 'none',
                duration: 2000
              })
            },
          })
        }
      })
  },*/

  getComonUrl:function(type,avb_SliceName,fileId,index){
    if(type==1){
      if(avb_SliceName==null || avb_SliceName ==undefined || avb_SliceName=="" || fileId==null || fileId ==undefined || fileId==""){
        return "";
      }
      return app.globalData.fileUrl+"/avarch/"+avb_SliceName+"/source/"+fileId+"/"+fileId+"-shuiyin.jpg?"+ Date.parse(new Date());
    }else if(type==2){
      if(avb_SliceName==null || avb_SliceName ==undefined || avb_SliceName==""){
        return "";
      }
      return app.globalData.fileUrl+"/singleProjectPath/houseHold/pmt"+avb_SliceName+".jpg?"+ Date.parse(new Date());
    }else if(type==3){
      if(avb_SliceName==null || avb_SliceName ==undefined || avb_SliceName=="" || fileId==null || fileId ==undefined || fileId==""){
        return "";
      }
      return app.globalData.fileUrl+"/eaorsPath/"+avb_SliceName+"/source/"+fileId+"/"+fileId+"-"+index+".png";
    }
  },
/**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.setStorageSync('guid', '');
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  
})